package com.atguigu.srb.core.controller.admin;


import com.atguigu.common.exception.BusinessAssert;
import com.atguigu.common.exception.BusinessException;
import com.atguigu.common.result.R;
import com.atguigu.common.result.ResponseEnum;
import com.atguigu.srb.core.anno.MyAnnotation4Swagger;
import com.atguigu.srb.core.pojo.entity.IntegralGrade;
import com.atguigu.srb.core.service.IntegralGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.print.attribute.standard.MediaSize;
import javax.swing.*;
import java.math.BigDecimal;
import java.util.List;

import static com.atguigu.common.result.ResponseEnum.BORROW_AMOUNT_NULL_ERROR;

/**
 * <p>
 * 积分等级表 前端控制器
 * </p>
 *
 * @author 龙龙龙
 * @since 2022-01-21
 *
 *
 */
@RestController
@RequestMapping("/admin/core/integralGrade")
//@CrossOrigin
@Api(tags = {"IntegralGradeController控制器"}) //tag可以写多个 会在ui控制器生成多个列表
public class IntegralGradeController {

    @Autowired
    private IntegralGradeService integralGradeService;

    @GetMapping("/list")
    @ApiOperation("获取所有积分等级列表")
    public R listAll() {
        List<IntegralGrade> list = integralGradeService.list();
        return R.ok().data("IntegralGradeList", list);
    }

    @ApiOperation(value ="根据id删除积分等级",notes = "这里是更详细的描述")
    @DeleteMapping("/remove/{id}")
    public R removeById(@ApiParam(name = "id",value = "积分等级 id", required = true)
                        @PathVariable Long id) {
        boolean b = integralGradeService.removeById(id);
        if (b) {
            return R.ok().data("deleteTag", b);
        } else {
            return R.error().data("errorMessage", "");
        }
    }

    @ApiOperation("新增积分等级")
    @PostMapping("/save")
    public R save(@ApiParam(value = "积分等级对象", required = true)
                  @RequestBody IntegralGrade integralGrade) {
        BigDecimal borrowAmount = integralGrade.getBorrowAmount();
        //如果借款额度为空就手动抛出一个自定义的异常！
        /*if(borrowAmount == null  || borrowAmount.intValue()<=0){
//            BORROW_AMOUNT_NULL_ERROR(-201, "借款额度不能为空"),
            throw new BusinessException(BORROW_AMOUNT_NULL_ERROR);
        }*/
        //优雅的判断
        BusinessAssert.notNull(borrowAmount, ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
        BusinessAssert.isTrue(0 != borrowAmount.intValue(),ResponseEnum.BORROW_AMOUNT_NULL_ERROR);

        boolean save = integralGradeService.save(integralGrade);
        if (save) {
            return R.ok();
        } else {
            return R.error();
        }
    }


    @ApiOperation("根据id查询积分等级")
    @GetMapping("/get/{id}")
    public R getById(@ApiParam(value = "数据id", required = true, example = "1")
                     @PathVariable("id") Long id) {
        IntegralGrade byId = integralGradeService.getById(id);
        if (byId != null) {
            return R.ok().data("IntegralGrade", byId);
        } else {
            return R.error().message("未查询到结果");
        }
    }

//    @MyAnnotation4Swagger //加上该注解 则swaggerAPI不显示该方法的描述
//    @ApiIgnore   和自定义注解意思一样
    @ApiOperation("根据id修改结果")
    @PutMapping("/update")
    public R updateById(@ApiParam(value = "积分等级对象", required = true)
                        @RequestBody IntegralGrade integralGrade) {
        boolean b = integralGradeService.updateById(integralGrade);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }
}

